<?php abstract class Options {
	protected static $Options = array (
		'c' => array (
			'default' => '/etc/overwatch/overwatch.ini',
			'usage'   => 'config file name',
		),
		'd' => array (
			'default' => FALSE,
			'usage'   => '(overrides -l) set debug level to maximum',
		),
		'h' => array (
			'default' => FALSE,
			'usage'   => 'print short help and exit',
		),
		'l' => array (
			'default' => LOG_WARNING,
			'usage'   => 'syslog level: increase this value to see more info and vice versa',
		),
		'n' => array (
			'default' => FALSE,
			'usage'   => 'do not fork, stay in foreground, log to stderr',
		),
		'p' => array (
			'default' => '/var/run/overwatch/pid',
			'usage'   => 'pidfile name',
		),
		'v' => array (
			'default' => FALSE,
			'usage'   => 'show version and exit',
		),
	);
	public static function Config   () {return (self::$Options ['c']['value']);}
	public static function NoFork   () {return (self::$Options ['n']['value']);}
	public static function PidFile  () {return (self::$Options ['p']['value']);}
	public static function LogLevel () {return (self::$Options ['l']['value']);}
	public static function LLSet    () {return (self::$Options ['l']['set']);}
	public static function Init () {
		$OptDef = '';
		foreach (self::$Options as $Letter => $Option) {
			$OptDef .= $Letter;
			is_bool ($Option ['default']) or $OptDef .= ':';
		}
		foreach ($Options = getopt ($OptDef) as $Letter => $Value) {
			if (! is_bool (self::$Options [$Letter]['default'])) {
				self::$Options [$Letter]['value'] = (is_array ($Value)) ? reset ($Value) : $Value;
			} else self::$Options [$Letter]['value'] = TRUE;
		}
		foreach (array_keys (self::$Options) as $Letter) if (! array_key_exists ('value', self::$Options [$Letter])) {
			self::$Options [$Letter]['set'] = FALSE;
			self::$Options [$Letter]['value'] = self::$Options [$Letter]['default'];
		} else self::$Options [$Letter]['set'] = TRUE;

		// special modes
		if (self::$Options ['v']['value']) {
			Out::Stdout ('overwatchd version '.Version::Get ());
			exit (0);
		}
		if (self::$Options ['h']['value']) {
			Out::Stdout ('overwatchd version '.Version::Get ());
			Out::Stdout ('Usage: overwatchd [<options>]');
			foreach (self::$Options as $Letter => $Value) {
				if (! is_bool ($Value ['default'])) {
					$Def = (strlen ($Value ['default'])) ? '"'.$Value ['default'].'"' : 'Empty';
					$Def = " (default: $Def)";
				} else $Def = '';
				Out::Stdout (" -$Letter    {$Value ['usage']}{$Def}");
			}
			exit (0);
		}

		// log level
		if (! self::$Options ['d']['value']) {
			$Level = self::$Options ['l']['value'];
			if ((0 == $Level) and ('0' !== $Level)) {
				if (is_array ($ULevel = Out::CheckLevel ($Level))) {
					Out::W (Msg::UnknownSyslogLvl, $Level, implode (', ', $ULevel));
					self::$Options ['l']['value'] = self::$Options ['l']['default'];
				} else self::$Options ['l']['value'] = constant ("LOG_$ULevel");
			} else self::$Options ['l']['value'] = (int) $Level;
		} else list (self::$Options ['l']['value'], self::$Options ['l']['set']) = array (LOG_DEBUG, TRUE);

		// debug output
		foreach (self::$Options as $Letter => $Option) {
			if (! is_bool ($Option ['value'])) {
				$Value = ($Option ['set']) ? '* had been set to ' : 'left default at ';
				$Value .= (strlen ($Option ['value'])) ? '"'.$Option ['value'].'"' : 'empty';
			} else $Value = ($Option ['value']) ? '* had been set' : 'left unset';
			Out::D (Msg::OptionDump, $Letter, $Value);
		}
	}
} ?>